package com.jsh.erp.datasource.mpj;

import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jsh.erp.utils.StringUtil;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.List;

public class BaseMapper<T> extends MPJLambdaWrapper<T> {
    public <X> BaseMapper<T> eqIsNotEmpty(SFunction<X, ?> column, Object val) {
        if (val != null) {
            if (val instanceof String) {
                String str = val.toString().trim();
                if (StringUtil.isNotEmpty(str)) {
                    this.eq(column, val);
                }
            }else if (val instanceof Integer || val instanceof Long || val instanceof Short || val instanceof Byte) {
                /*要不要考虑int类型*/
                Long longVal = Long.parseLong(val.toString());
                this.eq(column, longVal);
            }else if (val instanceof Float || val instanceof Double) {
                Double doubleVal = Double.parseDouble(val.toString());
                this.eq(column, doubleVal);
            }
        }
        return this;
    }

    /*不要用判断的形式 用重载的形式 定义两个方法 分别传入集合 和 数组*/
    public <X> BaseMapper<T> inIsNotEmpty(SFunction<X, ?> column, Collection val) {
        if (val != null && val.size() > 0) {
            this.in(column, val);
        }
        return this;
    }

    public <X> BaseMapper<T> inIsNotEmpty(SFunction<X, ?> column, Object... vals) {
        if (vals != null && vals.length > 0) {
            this.in(column, vals);
        }
        return this;
    }

    public <X> BaseMapper<T> likeIsNotEmpty(SFunction<X, ?> column, Object val) {
        if (val != null) {
            if (val instanceof String) {
                String str = val.toString().trim();
                if (StringUtil.isNotEmpty(str)) {
                    this.like(column, val);
                }
            }else if (val instanceof Integer || val instanceof Long || val instanceof Short || val instanceof Byte) {
                /*要不要考虑int类型*/
                Long longVal = Long.parseLong(val.toString());
                this.like(column, longVal);
            }else if (val instanceof Float || val instanceof Double) {
                Double doubleVal = Double.parseDouble(val.toString());
                this.like(column, doubleVal);
            }
        }
        return this;
    }

    public <X> BaseMapper<T> eqIsNotZero(SFunction<X, ?> column, Object val) {
        if (val != null) {
            if (val instanceof Integer || val instanceof Long || val instanceof Short || val instanceof Byte) {
                /*要不要考虑int类型*/
                Long longVal = Long.parseLong(val.toString());
                if (longVal != 0) {
                    this.eq(column, longVal);
                }
            }else if (val instanceof Float || val instanceof Double) {
                Double doubleVal = Double.parseDouble(val.toString());
                if (doubleVal != 0){
                    this.eq(column, doubleVal);
                }
            }
        }
        return this;
    }

    public BaseMapper<T> andTypeEqualTo(Object val){
        this.eq("type",val);
        return this;

    }
    public BaseMapper<T> andEnabledEqualTo(Object val){
        this.eq("enabled",val);
        return this;

    }
}
